package com.yxzx.api.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yxzx.api.dto.LessonListRes;
import com.yxzx.api.dto.RestStatusCode;
import com.yxzx.api.entity.Course;
import com.yxzx.api.entity.LessonPlan;
import com.yxzx.api.exception.CustomException;
import com.yxzx.api.service.ICourseService;
import com.yxzx.api.service.ILessonPlanService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 课时计划 前端控制器
 * </p>
 *
 * @author YWB
 * @since 2020-09-06
 */
@RestController
@RequestMapping("/api/lesson-plan")
public class LessonPlanController extends BaseController{

    @Autowired
    private ICourseService courseService;

    @Autowired
    private ILessonPlanService lessonPlanService;

    @GetMapping("/{courseId}")
    @ApiOperation("获取课程安排列表")
    public ResponseEntity<LessonListRes> getLessons(@PathVariable(value ="courseId",required = true) @ApiParam("课程ID")Long courseId){

        try{
            Course course=courseService.getById(courseId);


            LessonListRes listRes=new LessonListRes();
            if(course!=null) {
                listRes.setCourseHour(course.getCountHour());
                listRes.setCoursePrice(course.getPrice());
                listRes.setCourseId(course.getId());
                listRes.setSaleCount(course.getSaleCount());
                listRes.setIsBuy(isBuyCourse(courseId, getUser()));


                QueryWrapper<LessonPlan> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("course_id", courseId);
                List<LessonPlan> list = lessonPlanService.list(queryWrapper);

                listRes.setLessons(list);

                return ResponseEntity.ok(listRes);
            }else {
                throw new CustomException("",RestStatusCode.COURSE_LESSON_QUERY_ERROR.getCode(),RestStatusCode.COURSE_LESSON_QUERY_ERROR.getMessage());
            }
        }catch(CustomException e){
            e.printStackTrace();
            throw new CustomException("",RestStatusCode.COURSE_LESSON_QUERY_ERROR.getCode(),RestStatusCode.COURSE_LESSON_QUERY_ERROR.getMessage());

        }
    }

}
